package com.pitb.smsreceiver;

import java.util.StringTokenizer;

import org.json.JSONException;
import org.json.JSONObject;

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.telephony.SmsManager;
import android.text.TextUtils;
import android.util.Log;

public class SmsPostService extends Service{
	@Override
	public void onCreate() {
		
		super.onCreate();
	}

	@Override
	public IBinder onBind(Intent intent) {
		
		return null;
	}
	
	@Override
	public int onStartCommand(Intent intent, int flags, int startId) {	
		if(intent == null)
			return START_STICKY;
		String number = intent.getStringExtra(SmsReciever.NUMBER);
		String sms = intent.getStringExtra(SmsReciever.SMS);
		PostTask task = new PostTask(number, sms);
		task.start();
		return START_STICKY;
	}
	
	class PostTask extends Thread{
		String number ;
		String sms;
		public PostTask(String numberarg,String smsarg){
			number = numberarg ;
			sms = smsarg;
		}
		
		@Override
		public void run() {
			if(TextUtils.isEmpty(sms)){
				replyCommand(number,PATTERN);
				return;
			}
			if(!sms.contains(",")){
				replyCommand(number,PATTERN);
				return;
			}
			StringTokenizer tokens = new StringTokenizer(sms, ",");
			if(tokens.hasMoreElements()){
				String problem = tokens.nextElement().toString();
				String type = tokens.nextElement().toString();
				String address = tokens.nextElement().toString();
				String area = tokens.nextElement().toString();
				String city = tokens.nextElement().toString();
				String province = tokens.nextElement().toString();								
				String response = HttpPostUtil.postComplaint(problem, address, number, province, city, area, type);
				Log.i("Sms Register", "Server response is \n" + response);
				if(TextUtils.isEmpty(response)){
					return;
				}				
				JsonParser parse = JsonParser.parse(response);
				if(parse != null){
					if(parse.status.equals(JsonParser.ERROR)){
						replyCommand(number,PATTERN1);
						return;
					}
					if(parse.status.equals(JsonParser.SUCCESS)){
						replyCommand(number,"Successfully submitted compalint, You complaint id is " + parse.complaintId);						
						return;
					}
				}
			}
		}		
		
		void replyCommand(String number,String reply){
			SmsManager smsManager = SmsManager.getDefault();
			smsManager.sendTextMessage(number, null, reply, null, null);
		}
	}
	
	static class JsonParser {
		public static final String STATUS = "status";
		public static final String COMPLAINT_ID = "complaint-ID";
		public static final String MESSAGE = "message";
		
		public static final String ERROR = "Error";
		public static final String SUCCESS = "Success";
		
		String status ;
		String message ;
		String complaintId ;
		
		public static JsonParser parse(String response){			
			try {
				JSONObject json = new JSONObject(response);
				JsonParser parse = new JsonParser();
				parse.status = json.getString(STATUS);
				parse.message = json.getString(MESSAGE);
				parse.complaintId = json.getString(COMPLAINT_ID);
				return parse;
			} catch (JSONException e) {
				e.printStackTrace();
			}
			return null ;
		}
	}
	
	public static final String PATTERN = "Invalid Command!Use following pattern.. Problem,Type,Street address,Area,City,Province";
	public static final String PATTERN1 = "Invalid pattern!Please use following pattern.. Problem,Type,Street address,Area,City,Province";
}
